<template>
  <div class='search'>
    <f-drawer ref='orderDrawer' title='退货申请' width='80%'  @submit='handleSubmit' @visibleChange='visibleChange'>
      <back-form ref='form1' v-if='drawerVisible' :isEdit='isEdit' :formItem.sync='formItem' :processInstanceId='formData.processInstanceId' :stepIndex.sync='stepIndex'></back-form>
      <a-tooltip placement='topLeft' slot='drawerFooter'>
        <a-button @click='$refs.orderDrawer.close()' style='margin-right: 8px'>关闭</a-button>
        <a-button type='danger' :loading='loading' @click='handleSubmit(0)'>{{ btnText }}</a-button>
      </a-tooltip>
    </f-drawer>
  </div>
</template>
<script>
import BackForm from "./back_form.vue"

export default {
  name: "BackDrawer",
  props: {},
  data() {
    return {
      refs: this.$refs,
      drawerVisible: false,
      isEdit: false,
      title: "",
      stepIndex: 0,
      loading: true,
      btnText: "提交",
      isAdd: true,
      current: "form1",
      formData: {},
      formItem: {
        orderId: "",
        orderNo: "",
        delivery: "",
        saleManName: "",
        customName: "",
        processInstanceId: null,
        invoiceSendType: 1,
        reputation: 0,
        reputationAmount: 0,
        customShId: null,
        bankId: null,
        customBondType: 0,

        supplierBondType: 0,
        customId: "",
        billsType: "",
        remark: ""
      },
      forms: [
        "form1"
      ]
    }
  },
  methods: {
    showDrawer(data) {
      this.isEdit = false
      this.loading = false
      this.btnText = "保存订单"
      this.stepIndex = 0
      if (data && data.orderId) {
        this.isEdit = true
        this.formData = data
        this.processInstanceId = data.processInstanceId
      } else {
        this.formData = {parentId:data.parentId}
        this.processInstanceId = null
      }
      this.refs["orderDrawer"].show()
      this.tabClick("form1")
    },
    handleSubmit(opt) {
      this.loading = false
      this.refs[this.current].handleSubmit((data) => {
        this.$refs.orderDrawer.close()
      }, () => {
        this.btnText="保存中";
        this.loading = true;
        setTimeout(()=>{
          this.btnText="保存订单";
          this.loading = false;
        },10000)
        return true;
      })
    },
    clearForm() {
      localStorage.removeItem("orderFormJson")
      this.$nextTick(() => {
        this.$refs.form1.resetForm()
      })
    },
    visibleChange(drawerVisible) {
      this.drawerVisible = drawerVisible
      if (drawerVisible == false) {
        //刷新订单列表
        this.$emit("success")
      }
    },
    tabClick(name) {
      this.current = name
      this.$nextTick(() => {
        console.info("###################################################orderform", this.formData)
        this.refs[this.current].setData(this.formData)
      })
    }
  },
  created() {
  },
  components: { BackForm }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
